package com.dreamana.components
{
	import com.dreamana.ui.BaseComponent;
	import com.dreamana.ui.ISkinnable;
	import com.dreamana.ui.SkinGraphics;
	
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	public class TextInput extends BaseComponent implements ISkinnable
	{
		public static var defaultFormat:TextFormat = new TextFormat("Arial", 12, 0x0, null, null, null, null, null, TextFormatAlign.LEFT);
		
		protected var _back:Shape;
		protected var _backColor:int = -1;
		protected var _backTexture:BitmapData;
		protected var _backScale9Grid:Rectangle;
		protected var _textField:TextField;
		protected var _padding:Object;
		
		public function TextInput(w:int, h:int, format:TextFormat=null, text:String="")
		{
			_width = w;
			_height = h;
			_padding = {top:0, right:0, bottom:0, left:0};
			
			_back = new Shape();
			
			_textField = new TextField();
			_textField.type = TextFieldType.INPUT;
			_textField.selectable = true;
			_textField.defaultTextFormat = (format)? format : defaultFormat;
			_textField.text = text;
			
			_textField.width = _width-10;
			_textField.height = _height-10;
			
			this.init();
		}
		
		override protected function addChildren():void
		{
			this.addChild(_back);
			this.addChild(_textField);
		}
		
		//--- PUBLIC METHODS ---
		
		override public function draw():void
		{
			var g:Graphics = _back.graphics;
			
			g.clear();
			
			if(_backTexture) {
				if(_backScale9Grid) SkinGraphics.fill9Grid(g, _backTexture, _width, _height, _backScale9Grid, false);
				else SkinGraphics.fillRect(g, _backTexture, _width, _height);
			}
			else if(_backColor >= 0) {
				SkinGraphics.drawBlank(g, _width, _height, _backColor);
			}
			else {
				SkinGraphics.drawWireFrame(g, _width, _height);
			}
			
			_textField.x = _padding.left;
			_textField.y = _padding.top;
			_textField.width = _width - _padding.left - _padding.right;
			_textField.height = _height - _padding.top - _padding.bottom;
		}
		
		override public function setStyle(style:String, value:Object):void
		{
			super.setStyle(style, value);
			
			switch(style) {
				case "background-color":
					_backColor = value as Number;
					invalidate();
					break;
				case "background-image":
					_backTexture = value as BitmapData;
					invalidate();
					break;
				case "background-9grid":
					_backScale9Grid = value as Rectangle;
					invalidate();
					break;
				case "padding":
					setPadding(value as Number);
					break;
				case "padding-top":
					setPadding(value as Number, _padding.right, _padding.bottom, _padding.left);
					break;
				case "padding-right":
					setPadding(_padding.top, value as Number, _padding.bottom, _padding.left);
					break;
				case "padding-bottom":
					setPadding(_padding.top, _padding.right, value as Number, _padding.left);
					break;
				case "padding-left":
					setPadding(_padding.top, _padding.right, _padding.bottom, value as Number);
					break;
			}
		}
		
		/**
		 * padding
		 * @param args [padding | top, right, bottom, left]
		 * @return 
		 */		
		public function setPadding(...args):void
		{
			if(args.length == 1) {
				var p:int = args[0]; 
				_padding.top = p;
				_padding.right = p;
				_padding.bottom = p;
				_padding.left = p;
			}
			else if(args.length == 4) {
				_padding.top = args[0];
				_padding.right = args[1];
				_padding.bottom = args[2];
				_padding.left = args[3];
			}
			
			draw();
		}
		
		public function setTextFieldProps(params:Object):void
		{
			for(var k:String in params) {
				if(_textField.hasOwnProperty(k)) _textField[k] = params[k];
			}
		}
		
		//--- EVENT HANDLERS ---
		
		
		//--- GETTER/SETTERS ---
		public function get background():Shape { return _back; }
		
		public function get textField():TextField {	return _textField; }
		
		public function get text():String {	return _textField.text; }
		public function set text(value:String):void	{
			_textField.text = value;
		}
		
		public override function set enabled(value:Boolean):void {
			super.enabled = value;
			//TODO: state
			//this.alpha = value ? 1.0 : 0.5;
		}
	}
}